package com.iot.controller;


import com.iot.domain.Code;
import com.iot.domain.ResponseResult;
import com.iot.domain.TeacherUser;
import com.iot.domain.TeacherUserVo;
import com.iot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.context.annotation.SessionScope;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/*用户的操作*/
@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;



    /*登录*/
    @RequestMapping("/login")
    public String    login(HttpServletRequest request, TeacherUserVo teacherUserVo) throws Exception {


        if (teacherUserVo!=null){
            TeacherUser teacherUser = userService.login(teacherUserVo);


                HttpSession session = request.getSession();

                session.setAttribute("user",teacherUser);
                session.setAttribute("phone",teacherUser.getPhone());

             return  "forward:/main.jsp";

            }else {

            HttpSession session = request.getSession();

                return "redirect:/login.jsp";

            }

        }

        //注册用户
     @RequestMapping("/register")
    public  @ResponseBody ResponseResult register(TeacherUserVo teacherUserVo){


         Boolean  aBoolean = userService.register(teacherUserVo);

         if (aBoolean){
             return  new  ResponseResult(true,1,"注册成功",null);
         }else {
             return  new ResponseResult(false,0,"注册失败",null);
         }

     }

    /*查询是否存在手机号*/
    @RequestMapping("/checkAccount")
    public  @ResponseBody ResponseResult  checkAccount(String phone){

        TeacherUser userByPhone = userService.findUserByPhone(phone);

        if (userByPhone==null){

            return  new ResponseResult(true,1,"手机号不存在，可以注册",null);
        }else {

            return new ResponseResult(true,0,"手机号已经存在,请重新输入",null);

        }

    }


    /*切换用户*/
    @RequestMapping("/changeUser")
    public  String  changeUser(HttpServletRequest request, HttpServletResponse response) throws IOException {

        HttpSession session = request.getSession();
        session.invalidate();

        return "redirect:/login.jsp";
    }


    /*退出登录*/
    @RequestMapping("/outLogin")
    public   void outLoggin(HttpServletRequest request, HttpServletResponse response) throws IOException {

        HttpSession session = request.getSession();
        session.invalidate();
        response.sendRedirect("../login.jsp");

    }

    /*注销账户*/
    @RequestMapping("/cancelUser")
    public  String  cancelUser(String  phone){

        if (phone==null){

            return "redirect:/login.jsp";

        }else {
            //删除账户
            userService.cancelUser(phone);
            return "redirect:/login.jsp";
        }
    }

}



